﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LAVAA.BL.Control;
using LAVAA.WEB.Models;

namespace LAVAA.WEB.Controllers
{
    public class ActivityTypeController : Controller
    {

        UserCase01 _context = new UserCase01();
        //
        // GET: /ActivityType/

        public ActionResult Index()
        {
            var a = _context.FindActivityTypes();
            var types = new List<ActivityTypeViewModel>();
            foreach (var b in a)
            {
                types.Add(b.ToViewModel());
            }
            return View(types);
        }

        //
        // GET: /ActivityType/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /ActivityType/Create

        [HttpPost]
        public ActionResult Create(ActivityTypeViewModel activityTypeViewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _context.CreateActivityType(activityTypeViewModel.ToDAL());
                    return RedirectToAction("Index");
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
            return View(activityTypeViewModel);


        }

        //
        // GET: /ActivityType/Edit/5

        public ActionResult Edit(Guid id)
        {
            var a = _context.FindActivityTypesById(id).ToViewModel();
            return View(a);
        }

        //
        // POST: /ActivityType/Edit/5

        [HttpPost]
        public ActionResult Edit(Guid id, ActivityTypeViewModel activityTypeViewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _context.UpdateActivityType(activityTypeViewModel.ToDAL());
                    return RedirectToAction("Index");
                }
                catch(Exception ex)
                {
                    ModelState.AddModelError(string.Empty, ex.Message);
                }
            }
            return View(activityTypeViewModel);
        }

        //
        // GET: /ActivityType/Delete/5

        public ActionResult Delete(Guid id)
        {
            var a = _context.FindActivityTypesById(id).ToViewModel();
            return View(a);
        }

        //
        // POST: /ActivityType/Delete/5

        [HttpPost]
        public ActionResult Delete(Guid id, ActivityTypeViewModel activityTypeViewModel)
        {
            try
            {
                _context.DeleteActivityType(id);
                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }
            return View();
        }
    }
}
